/* 
 *  Syntelos XB
 *  Copyright (C) 2006 John Pritchard.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307 USA
 */

package syntelos.xb;

/** 
 * 
 * 
 * @author jdp
 */
public class Methods 
    extends Base
{
    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb","xb:methods");

    public Methods(org.w3c.dom.Document doc, syntelos.iou.dom.Name name){
        super(doc,name);
    }
    public Methods(org.w3c.dom.Document doc){
        this(doc,NAME);
    }
    public Methods(){
        this(DOC_NIL);
    }

    public final syntelos.xb.Class getDeclaringClass(){
        return (syntelos.xb.Class)this.getParentNode();
    }

    /**
     * @param ref XB Method reference
     * @return Referenced method in this class or null
     */
    public final Method getMethod(Uri ref){
        if (null == ref)
            return null;
        else {
            java.lang.String name_value = ref.getQuery("name");
            if (null == name_value)
                throw new Error.State("Missing reference name in '"+ref.toString()+"'.");
            else {
                /*
                 * TODO: Use type info to differentiate overloaded
                 *       method names.
                 */
                return (Method)this.getChildByField("name",name_value);
            }
        }
    }
    public void readCF(IO.Uri src, CF.Reader reader)
        throws java.io.IOException
    {
        org.w3c.dom.Document doc = this.getOwnerDocument();
        int count = reader.readUnsignedShort();
        Method method;
        for (int cc = 0; cc < count; cc++){
            method = new Method(doc);
            this.appendChild(method);
            method.readCF(src,reader);
        }
    }
    public void writeCF(IO.Uri dst, CF.Writer writer)
        throws java.io.IOException
    {
        org.w3c.dom.NodeList children = this.getChildNodes();
        int count = children.getLength();
        writer.writeShort(count);
        Method method;
        for (int cc = 0; cc < count; cc++){
            method = (Method)children.item(cc);
            method.writeCF(dst,writer);
        }
    }
}
